package com.amap.api.col.s;

import com.amap.api.col.s.dq;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.json.JSONObject;

/* compiled from: LogEngine.java */
/* loaded from: classes.dex */
public final class ec {
    public static void a(String str, byte[] bArr, eb ebVar) throws IOException, CertificateException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException {
        dq dqVar;
        OutputStream outputStream = null;
        try {
            if (a(ebVar.a, str)) {
                return;
            }
            File file = new File(ebVar.a);
            if (!file.exists()) {
                file.mkdirs();
            }
            dqVar = dq.a(file, ebVar.b);
            try {
                dqVar.a(ebVar.d);
                byte[] b = ebVar.e.b(bArr);
                dq.a b2 = dqVar.b(str);
                outputStream = b2.a();
                outputStream.write(b);
                b2.b();
                dqVar.b();
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                }
                try {
                    dqVar.close();
                } catch (Throwable th2) {
                    th2.printStackTrace();
                }
            } catch (Throwable th3) {
                th = th3;
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (Throwable th4) {
                        th4.printStackTrace();
                    }
                }
                if (dqVar != null) {
                    try {
                        dqVar.close();
                    } catch (Throwable th5) {
                        th5.printStackTrace();
                    }
                }
                throw th;
            }
        } catch (Throwable th6) {
            th = th6;
            dqVar = null;
        }
    }

    public static int a(eb ebVar) {
        dq dqVar = null;
        try {
            try {
                if (ebVar.f.c()) {
                    ebVar.f.a(true);
                    dq a = dq.a(new File(ebVar.a), ebVar.b);
                    try {
                        ArrayList arrayList = new ArrayList();
                        byte[] a2 = a(a, ebVar, arrayList);
                        if (a2 != null && a2.length != 0) {
                            de deVar = new de(a2, ebVar.c);
                            dt.a();
                            JSONObject jSONObject = new JSONObject(new String(dt.a(deVar).a));
                            if (jSONObject.has("code") && jSONObject.getInt("code") == 1) {
                                if (ebVar.f != null && a2 != null) {
                                    ebVar.f.a(a2.length);
                                }
                                if (ebVar.f.b() < Integer.MAX_VALUE) {
                                    a(a, arrayList);
                                } else {
                                    a.c();
                                }
                                return a2.length;
                            }
                            dqVar = a;
                        }
                        try {
                            a.close();
                        } catch (Throwable th) {
                            th.printStackTrace();
                        }
                        return -1;
                    } catch (Throwable th2) {
                        th = th2;
                        dqVar = a;
                        try {
                            df.c(th, "leg", "uts");
                            if (dqVar != null) {
                                dqVar.close();
                            }
                            return -1;
                        } catch (Throwable th3) {
                            if (dqVar != null) {
                                try {
                                    dqVar.close();
                                } catch (Throwable th4) {
                                    th4.printStackTrace();
                                }
                            }
                            throw th3;
                        }
                    }
                }
            } catch (Throwable th5) {
                th = th5;
            }
            if (dqVar != null) {
                dqVar.close();
            }
        } catch (Throwable th6) {
            th6.printStackTrace();
        }
        return -1;
    }

    private static byte[] a(dq dqVar, eb ebVar, List<String> list) {
        String[] list2;
        try {
            File a = dqVar.a();
            if (a != null && a.exists()) {
                int i = 0;
                for (String str : a.list()) {
                    if (str.contains(".0")) {
                        String str2 = str.split("\\.")[0];
                        byte[] a2 = eh.a(dqVar, str2);
                        i += a2.length;
                        list.add(str2);
                        if (i > ebVar.f.b()) {
                            break;
                        }
                        ebVar.g.b(a2);
                    }
                }
                if (i <= 0) {
                    return null;
                }
                return ebVar.g.a();
            }
        } catch (Throwable th) {
            df.c(th, "leg", "gCo");
        }
        return new byte[0];
    }

    private static void a(dq dqVar, List<String> list) {
        if (dqVar != null) {
            try {
                for (String str : list) {
                    dqVar.c(str);
                }
                dqVar.close();
            } catch (Throwable th) {
                df.c(th, "ofm", "dlo");
            }
        }
    }

    private static boolean a(String str, String str2) {
        try {
            return new File(str, str2 + ".0").exists();
        } catch (Throwable th) {
            df.c(th, "leg", "fet");
            return false;
        }
    }
}
